﻿using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text;
using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.ViewFeatures;

namespace MyUtility.EasyHtmlHelper {
    public static class EasyHtmlHelperInput {
        #region Inpu输入框
        public static IHtmlContent DateTextBox(this HtmlHelper html, string id) {
            TagBuilder tag = new TagBuilder("date");
            //tag.MergeAttribute("id", id);
            //tag.MergeAttribute("name", id);
            //tag.MergeAttribute("class", "easyui-my97\" data-options=\"dateFmt: 'yyyy-MM-dd'");

            string sHtml = "<input id='" + id + "' name='" + id + "'  class=\"easyui-my97\" data-options=\"dateFmt: 'yyyy-MM-dd'\" > ";
            tag.InnerHtml.Append(sHtml);
            return new HtmlString(tag.ToString());
        }
        /// <summary>
        /// 日期格式的input
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="htmlHelper"></param>
        /// <param name="expression"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="dateFmt">日期格式默认"yyyy-MM-dd"</param>
        /// <returns></returns>
        public static IHtmlContent EasyDateTextBoxFor<TModel, TResult>(this IHtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TResult>> expression, string width = "80%", string height = "25px", string dateFmt = "yyyy-MM-dd") {
            if (htmlHelper == null) {
                throw new ArgumentNullException(nameof(htmlHelper));
            }

            if (expression == null) {
                throw new ArgumentNullException(nameof(expression));
            }
            string style = $"width:{width};height:{height};border-radius:5px;border:1px solid #808080;";
            string fmt = $"dateFmt:'{dateFmt}'";
            return htmlHelper.TextBoxFor(expression, new { @class = "easyui-my97", @style = style, data_options = fmt, onfocus = htmlHelper.Raw("this.blur()") });

        }
        /// <summary>
        /// EasyUi TextBox格式
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="htmlHelper"></param>
        /// <param name="expression"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="required">是否必填栏位</param>
        /// <param name="prompt">默认提示文字</param>
        /// <param name="read_only">是否只读 默认false</param>
        /// <returns></returns>
        public static IHtmlContent EasyTextBoxFor<TModel, TResult>(this IHtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TResult>> expression, string width = "80%", string height = "25px", bool required = false, string prompt = "", bool read_only = false) {
            if (htmlHelper == null) {
                throw new ArgumentNullException(nameof(htmlHelper));
            }

            if (expression == null) {
                throw new ArgumentNullException(nameof(expression));
            }

            string style = $"width:{width};height:{height};";
            string requiredStr = required == true ? "true" : "false";
            string readonlyStr = read_only == true ? "true" : "false";
            string options = $"required:{requiredStr},prompt:'{prompt}',editable:{readonlyStr}";
            TagBuilder tag = new TagBuilder("text");
            return htmlHelper.TextBoxFor(expression, new { @class = "easyui-textbox", @style = style, data_options = options });
        }
        /// <summary>
        /// EasyUi TextBox格式 Required
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="htmlHelper"></param>
        /// <param name="expression"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="prompt">默认提示文字</param>
        /// <param name="read_only">是否只读 默认false</param>
        /// <returns></returns>
        public static IHtmlContent EasyRequiredTextBoxFor<TModel, TResult>(this IHtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TResult>> expression, string width = "80%", string height = "25px", string prompt = "", bool read_only = false) {
            if (htmlHelper == null) {
                throw new ArgumentNullException(nameof(htmlHelper));
            }

            if (expression == null) {
                throw new ArgumentNullException(nameof(expression));
            }

            string style = $"width:{width};height:{height};";
            string readonlyStr = read_only == true ? "true" : "false";
            string options = $"required:true,prompt:'{prompt}',editable:{readonlyStr}";
            TagBuilder tag = new TagBuilder("text");
            return htmlHelper.TextBoxFor(expression, new { @class = "easyui-textbox", @style = style, data_options = options });
        }
        /// <summary>
        /// EasyUi TextBox格式 只读
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="htmlHelper"></param>
        /// <param name="expression"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="required">是否必填 默认false</param>
        /// <param name="prompt">默认提示文字</param>
        /// <returns></returns>
        public static IHtmlContent EasyReadOnlyTextBoxFor<TModel, TResult>(this IHtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TResult>> expression, string width = "80%", string height = "25px", bool required = false, string prompt = "") {
            if (htmlHelper == null) {
                throw new ArgumentNullException(nameof(htmlHelper));
            }

            if (expression == null) {
                throw new ArgumentNullException(nameof(expression));
            }

            string style = $"width:{width};height:{height};";
            string requiredStr = required == true ? "true" : "false";
            string options = $"editable:false,required:{requiredStr},prompt:'{prompt}'";
            TagBuilder tag = new TagBuilder("text");
            return htmlHelper.TextBoxFor(expression, new { @class = "easyui-textbox", @style = style, data_options = options });
        }
        /// <summary>
        /// EasyUi 数字格式输入框 options可以指定格式默认min:0,precision:0
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="htmlHelper"></param>
        /// <param name="expression"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="options">格式配置选项默认min:0,precision:0 可以参考EasyUI文档进行设置</param>
        /// <param name="required">是否必填栏位</param>
        /// <param name="prompt">默认提示文字</param>
        /// <returns></returns>
        public static IHtmlContent EasyNumberBoxFor<TModel, TResult>(this IHtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TResult>> expression, string width = "80%", string height = "25px", string options = "min:0,precision:0", bool required = false, string prompt = "") {
            if (htmlHelper == null) {
                throw new ArgumentNullException(nameof(htmlHelper));
            }

            if (expression == null) {
                throw new ArgumentNullException(nameof(expression));
            }

            string style = $"width:{width};height:{height};";
            string requiredStr = required == true ? "true" : "false";
            options = $"{options},required:{requiredStr},prompt:'{prompt}'";
            TagBuilder tag = new TagBuilder("text");
            return htmlHelper.TextBoxFor(expression, new { @class = "easyui-numberbox", @style = style, data_options = options });
        }
        /// <summary>
        /// EasyUi TextArea格式 多行文本
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="htmlHelper"></param>
        /// <param name="expression"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="required">是否必填栏位</param>
        /// <returns></returns>
        public static IHtmlContent EasyTextAreaFor<TModel, TResult>(this IHtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TResult>> expression, string width = "80%", string height = "25px", bool required = false, string prompt = "", string label = "", string labelPosition = "top") {
            if (htmlHelper == null) {
                throw new ArgumentNullException(nameof(htmlHelper));
            }

            if (expression == null) {
                throw new ArgumentNullException(nameof(expression));
            }

            string style = $"width:{width};height:{height};";
            string requiredStr = required == true ? "true" : "false";
            string options = $"required:{requiredStr},multiline:true,label:'{label}',labelPosition:'{labelPosition}',prompt:'{prompt}'";
            TagBuilder tag = new TagBuilder("text");
            return htmlHelper.TextBoxFor(expression, new { @class = "easyui-textbox", @style = style, data_options = options });
        }
        #endregion

        #region Input输入框不带expression表达式
        /// <summary>
        /// EasyUi TextBox格式
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="expression"></param>
        /// <param name="required">是否必填 默认false</param>
        /// <param name="prompt">提示文本</param>
        /// <param name="read_only">是否只读 默认false</param>
        /// <returns></returns>
        public static IHtmlContent EasyTextBox(this IHtmlHelper htmlHelper, string expression, object value = null, string width = "80%", string height = "25px", bool required = false, string prompt = "", bool read_only = false) {
            if (htmlHelper == null) {
                throw new ArgumentNullException(nameof(htmlHelper));
            }
            string style = $"width:{width};height:{height};";
            string requiredStr = required == true ? "true" : "false";
            string readonlyStr = read_only == true ? "true" : "false";
            string options = $"required:{requiredStr},prompt:'{prompt}',editable:{readonlyStr}";
            TagBuilder tag = new TagBuilder("text");
            return htmlHelper.TextBox(expression, value, new { @class = "easyui-textbox", @style = style, data_options = options });
            //return htmlHelper.TextBox(expression, value: null, format: null, htmlAttributes: null);
        }
        /// <summary>
        /// EasyUi TextBox格式 Required
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="htmlHelper"></param>
        /// <param name="expression"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="prompt">默认提示文字</param>
        /// <param name="read_only">是否只读 默认false</param>
        /// <returns></returns>
        public static IHtmlContent EasyrRquiredTextBox(this IHtmlHelper htmlHelper, string expression, object value = null, string width = "80%", string height = "25px", string prompt = "", bool read_only = false) {
            if (htmlHelper == null) {
                throw new ArgumentNullException(nameof(htmlHelper));
            }
            string style = $"width:{width};height:{height};";
            string readonlyStr = read_only == true ? "true" : "false";
            string options = $"required:true,prompt:'{prompt}',editable:{readonlyStr}";
            TagBuilder tag = new TagBuilder("text");
            return htmlHelper.TextBox(expression, value, new { @class = "easyui-textbox", @style = style, data_options = options });
            //return htmlHelper.TextBox(expression, value: null, format: null, htmlAttributes: null);
        }

        /// <summary>
        /// EasyUi ReadonlyTextBox格式
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="expression"></param>
        /// <param name="required">是否必填 默认false</param>
        /// <param name="prompt">提示文本</param>
        /// <returns></returns>
        public static IHtmlContent EasyReadonlyTextBox(this IHtmlHelper htmlHelper, string expression, object value = null, string width = "80%", string height = "25px", bool required = false, string prompt = "") {
            if (htmlHelper == null) {
                throw new ArgumentNullException(nameof(htmlHelper));
            }
            string style = $"width:{width};height:{height};";
            string requiredStr = required == true ? "true" : "false";
            string options = $"editable:false,required:{requiredStr},prompt:'{prompt}'";
            TagBuilder tag = new TagBuilder("text");
            return htmlHelper.TextBox(expression,value, new { @class = "easyui-textbox", @style = style, data_options = options });
            //return htmlHelper.TextBox(expression, value: null, format: null, htmlAttributes: null);
        }
        /// <summary>
        /// EasyUi 数字格式输入框 options可以指定格式默认min:0,precision:0
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="htmlHelper"></param>
        /// <param name="expression"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="options">格式配置选项默认min:0,precision:0 可以参考EasyUI文档进行设置</param>
        /// <param name="required">是否必填栏位</param>
        /// <param name="prompt">默认提示文字</param>
        /// <returns></returns>
        public static IHtmlContent EasyNumberBox(this IHtmlHelper htmlHelper, string expression, object value = null, string width = "80%", string height = "25px", string options = "min:0,precision:0", bool required = false, string prompt = "") {
            if (htmlHelper == null) {
                throw new ArgumentNullException(nameof(htmlHelper));
            }

            string style = $"width:{width};height:{height};";
            string requiredStr = required == true ? "true" : "false";
            options = $"{options},required:{requiredStr},prompt:'{prompt}'";
            TagBuilder tag = new TagBuilder("text");
            return htmlHelper.TextBox(expression,value, new { @class = "easyui-numberbox", @style = style, data_options = options });
        }

        /// <summary>
        /// EasyUi TextArea格式 多行文本
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="htmlHelper"></param>
        /// <param name="expression"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="required">是否必填栏位</param>
        /// <param name="prompt">提示文本</param>
        /// <returns></returns>
        public static IHtmlContent EasyTextArea(this IHtmlHelper htmlHelper, string expression, object value, string width = "80%", string height = "25px", bool required = false, string prompt = "", string label = "", string labelPosition = "top") {
            if (htmlHelper == null) {
                throw new ArgumentNullException(nameof(htmlHelper));
            }

            string style = $"width:{width};height:{height};";
            string requiredStr = required == true ? "true" : "false";
            string options = $"required:{requiredStr},multiline:true,label:'{label}',labelPosition:'{labelPosition}',prompt:'{prompt}'";
            TagBuilder tag = new TagBuilder("text");
            return htmlHelper.TextBox(expression,value, new { @class = "easyui-textbox", @style = style, data_options = options });
        }
        #endregion
    }
}
